package cn.lidongyu.org.service.impl;

import cn.lidongyu.basic.PageList;
import cn.lidongyu.basic.constance.Constant;
import cn.lidongyu.org.domain.Department;
import cn.lidongyu.org.mapper.DepartmentMapper;
import cn.lidongyu.org.query.DepartmentQuery;
import cn.lidongyu.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Override
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }

    @Override
    public Department findOne(Long id) {
        return departmentMapper.findOne(id);
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY,key="'tree'")
    public void add(Department department) {
        departmentMapper.add(department);


    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY,key="'tree'")
    public void delete(Long id) {
        departmentMapper.delete(id);

    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY,key="'tree'")
    public void update(Department department) {

        String dirPath="/";
        if(department.getParent()==null){
            dirPath=dirPath+department.getId();
        }else{
            Department parent = departmentMapper.findOne(department.getParent().getId());
            dirPath=parent.getDirPath()+"/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);

    }

    @Override
    public Long findCount(DepartmentQuery departmentQuery) {
        return departmentMapper.findCount(departmentQuery);
    }

    @Override
    public PageList<Department> findByQuery(DepartmentQuery departmentQuery) {
        Long count = departmentMapper.findCount(departmentQuery);
        List<Department> query = departmentMapper.findByQuery(departmentQuery);
        PageList<Department> departmentPageList = new PageList<>(count,query);



        return departmentPageList;
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY,key="'tree'")
    public void deletemore(List<Long> ids) {
        departmentMapper.deletemore(ids);
    }

    @Override
    @Cacheable(cacheNames =  Constant.DEPT_TREE_KEY ,key="'tree'")
    public List<Department> QueryTree() {
        return getDepartmentsFromDb();
    }


    private List<Department> getDepartmentsFromDb() {
        List<Department> departments = departmentMapper.findAll();
        List<Department> DeptResult=new ArrayList<>();
        for(Department dept:departments){
            if(dept.getParent_id()==null){
                DeptResult.add(dept);
            }else {
                for(Department parent:departments){
                    if(parent.getId().equals(dept.getParent_id())){


                        parent.getChildren().add(dept);
                    }
                }
            }
        }
        return DeptResult;
    }
}
